package cn.jee.jsp;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@WebServlet("/delClazz")
public class DelClazzServlet  extends HttpServlet {
//  String driver = "org.hsqldb.jdbc.JDBCDriver";
    String driver = "com.p6spy.engine.spy.P6SpyDriver";
//  String url = "jdbc:hsqldb:hsql://localhost/";
  String url = "jdbc:p6spy:hsqldb:hsql://localhost/";
  String user = "sa";
  String pass = "";
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doPost(req,resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String clazz_id=req.getParameter("clazz_id");
    int row=-1;
    try{
      Class.forName(driver);
      String sql="delete from clazz where id=?";
      try(Connection con= DriverManager.getConnection(url,user,pass);
          PreparedStatement pstmt=con.prepareStatement(sql);){
        pstmt.setString(1,clazz_id);
        row=pstmt.executeUpdate();
      }
    }catch(Exception ex){
      throw new ServletException(ex);
    }
    HttpSession session=req.getSession();
    if(row>0){
      session.setAttribute("clazz_msg","删除班级成功");
    }else{
      session.setAttribute("clazz_msg","删除班级失败");
    }
    resp.sendRedirect(req.getContextPath()+"/showClazzes");
  }
}
